package com.mo.demo.guava;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class GuavaTest {
    public static void main(String[] args) {

        //最大只存2个，默认会采用 LRU 算法来淘汰
        Cache<String, String> cache = CacheBuilder.newBuilder().maximumSize(2).build();


        cache.put("1","111");
        cache.put("2","222");
        cache.put("3","333");

        for (int i = 0; i < 3; i++) {
            log.info("第{}个值：{}",i+1,cache.getIfPresent((i+1)+""));
        }

        // 输出：
        // 14:04:54.883 [main] INFO com.mo.demo.guava.GuavaTest - 第1个值：null
        // 14:04:54.893 [main] INFO com.mo.demo.guava.GuavaTest - 第2个值：222
        // 14:04:54.894 [main] INFO com.mo.demo.guava.GuavaTest - 第3个值：333
    }
}
